//定义app参数名称，并加载其它模块
var authApp=angular.module('authapp', ["ngCookies",'ngFileUpload','ui.bootstrap']);
//定义app参数名称，并加载其它模块
authApp.value('page','material_apply'); 

//定义父controller，主要用于controller之间传递信息
authApp.controller("fcontroller",function($scope,$http){
	//
	//****与分页相关的****
	//
	
	//从接口中获取总的分页数量
	$scope.$on('$getPageCountFromApi',function(event,data){
		$scope.$broadcast('$getPageCount',data);
	});
	
	//捕获分页点击事件并传递currentPage信息到主controller
	 $scope.$on('$pageAction',function(event,data){
		 $scope.$broadcast('$pageChanged',data);
	 });
})


authApp.controller("applySLMController",function($scope,$http,$location,$getQueryString,$splice){
	
	//**********主体方法--------------cm
	//设置分页初始参数（最好与$scope.currentPage和$scope.itemsPerPage一致！！！！//这边之所以$scope.pageNumber=0是因为后台接口第一页为0）
	$scope.preNum=$getQueryString("preNum");
	$scope.isAddM=true;
	$scope.showEdit = false;
	
	$scope.ifFirstTimeQuery=true;
	$scope.pageNumber=0;
	$scope.pageSize=10;
	//var requestJson={'pageNumber':$scope.pageNumber,'pageSize':$scope.pageSize,'fid':$getQueryString("pid"),'type':'1'};
	$scope.getAML=function(){
		$http({
			method:'post',
			url:apis.queryItem,
			data:{'pageNumber':$scope.pageNumber,'pageSize':$scope.pageSize,'fid':$getQueryString("pid"),'type':'1'},
		}).success(function(response){
			if(response.code=="000000"){
				$scope.list=response.data.data;
				$scope.totalCount = response.data.recordCount;
			    $scope.pageSize = response.data.pageSize;
			    
				angular.forEach($scope.list,function(data,index,array){
					if(data.major=="0"){
						data.majorName="学前教育";
					}else if(data.major=="1"){
						data.majorName="小学教育";
					}else if(data.major=="2"){
						data.majorName="中学教育";
					}else{
						data.majorName="未选择";
					}
				});
				//将返回的pagecount和current传到上面的controller
				$scope.$emit('$getPageCountFromApi',response.data);
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("服务器打瞌睡了，请稍等！");
		})
	}
	//捕捉分页点击事件并做处理
	$scope.$on("$pageChanged",function(event,data){
		if(!$scope.ifFirstTimeQuery){//解决第一次会调用两次接口的问题
			$scope.pageNumber=data.currentPage-1;//这边之所以-1是因为后台接口第一页为0
			console.log(data);
			console.log($scope.pageNumber);
			$scope.getAML();
		}
		$scope.ifFirstTimeQuery=false;//改变是否是第一次调用接口的状态
	})
	
	$scope.getAML();
	
	
	$scope.addM=function(){
		$scope.isAddM=false;
	}
	$scope.cancelAddM=function(){
		$scope.isAddM=true;
		$scope.addFZ="";
		$scope.addFZNum="";
		$scope.zbMajor="";
		$scope.addBZ="";
	}
	$scope.addFirstZ=function(){
		if(window.confirm("是否确认添加？")){
			if(!$scope.addFZNum){
				alert("请填写序号");
				return
			}
			if(!$scope.zbMajor){
				alert("请选择专业");
				return
			}
			if($scope.addFZ){
				if($scope.addBZ){
					var rJson={'standards':$scope.addBZ,'content':$scope.addFZ,'fid':$getQueryString("pid"),'type':'1','number':'0','number':$scope.addFZNum,'major':$scope.zbMajor};
					$http({
						method:'post',
						url:apis.insertItem,
						data:rJson,
					}).success(function(response){
						if(response.code=="000000"){
							$scope.isAddM=true;
							$scope.getAML();
							$scope.addFZ="";
							$scope.addFZNum="";
							$scope.zbMajor="";
							$scope.addBZ="";
						}
					}).error(function(){
						alert("服务器打瞌睡了，请稍等！");
						$scope.isAddM=true;
					})
				}else{
					alert("请输入二级指标标准");
				}
			}else{
				alert("请输入二级指标标题");
			}
		}
	}
	//编辑
	$scope.edit = function (item) {
        $scope.item = item;
        $scope.showEdit = true;
    }
    $scope.doEdit = function (item) {
		if(!item.number){
			alert("请填写序号");
			return;
		}
		if(!item.major){
			alert("请选择专业");
			return;
		}
		if(!item.content) {
			alert("请输入二级指标标题");
			return;
		}
		if(!item.standards) {
			alert("请输入二级指标标准");
			return;
		}
		var rJson={'standards':item.standards,'content':item.content,'pid':item.pid,'number':item.number,'major':item.major};
		$http({
			method:'post',
			url:apis.updateItem,
			data:rJson,
		}).success(function(response){
			if(response.code=="000000"){
                alert("编辑成功");
				$scope.showEdit=false;
				$scope.getAML();
				$scope.item="";
			}
		}).error(function(){
			alert("服务器打瞌睡了，请稍等！");
			$scope.showEdit=false;
		})
    }
    $scope.doCancel = function () {
        $scope.showEdit=false;
    }
	
	$scope.delCollect=function(pid){
		if(window.confirm("是否确定删除？")){
			var rJson={'pid':pid};
			$http({
				method:'post',
				url:apis.deleteItem,
				data:rJson,
			}).success(function(response){
				if(response.code=="000000"){
					$scope.getAML();
				}else{
					alert(response.errorMsg);
				}
			})
		}
	}
	$scope.turnTo=function(pid,major,num){
		location.href="m-detail.html?pid="+pid+"&major="+major+"&preNum="+num;
	}
	
})

//分页controller
authApp.controller('pageDemo',function($scope){
	//定义分页的初始参数（不改动，不会影响功能）
	$scope.currentPage=1;
	$scope.itemsPerPage=10;
	$scope.maxSize = 8;
	//需要接口返回的一些参数，如pagecount
	$scope.$on('$getPageCount',function(event,data){
        $scope.totalItems = parseInt(data.recordCount);
        $scope.bigTotalItems =parseInt(data.recordCount);      
	});
	//分页点击的触发事件
	 $scope.$watch('currentPage', function() {
		 var pagedata={currentPage:$scope.currentPage};
		 $scope.$emit('$pageAction',pagedata);
	  });
})
	